package com.shenyaocn.android.dualcamera;

import android.graphics.SurfaceTexture;
import android.hardware.usb.UsbDevice;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Surface;

import com.rscja.deviceapi.OTG;
import com.serenegiant.usb.USBMonitor;
import com.serenegiant.usb.UVCCamera;
import com.serenegiant.widget.UVCCameraTextureView;

import java.util.List;

public class SingleAct extends AppCompatActivity {
    private UVCCameraTextureView camera;
    private USBMonitor monitor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single);
        OTG.getInstance().on();
        camera= (UVCCameraTextureView) findViewById(R.id.mCameraView);
        monitor=new USBMonitor(this,new Deviceimpl());
    }
    class Deviceimpl implements USBMonitor.OnDeviceConnectListener{

        @Override
        public void onAttach(UsbDevice usbDevice) {
            final List<UsbDevice> list =monitor.getDeviceList();
            monitor.requestPermission(list.get(0));

//            if(list.size() > 1)
//                new Handler().postDelayed(new Runnable() {public void run() {monitor.requestPermission(list.get(1));}}, 200);
        }

        @Override
        public void onDettach(UsbDevice usbDevice) {

        }

        @Override
        public void onConnect(UsbDevice usbDevice, USBMonitor.UsbControlBlock usbControlBlock, boolean b) {
            UVCCamera ca = new UVCCamera();
            ca.open(usbControlBlock);
            ca.setPreviewSize(640,480,0,0.5f);
            SurfaceTexture st = camera.getSurfaceTexture();
            Surface surf = new Surface(st);
            ca.setPreviewDisplay(surf);
            ca.startPreview();
        }

        @Override
        public void onDisconnect(UsbDevice usbDevice, USBMonitor.UsbControlBlock usbControlBlock) {

        }

        @Override
        public void onCancel(UsbDevice usbDevice) {

        }
    }
}
